package logindemo.demo.controller;

import logindemo.demo.pojo.User;
import logindemo.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Map<String,String> login(User user, HttpServletRequest request){
        User login = userService.login(user.getUsername(), user.getPassword());
        HashMap<String, String> map = new HashMap<>();
        if(login != null){
            String username = login.getUsername();
            HttpSession session = request.getSession();
            request.getSession().setAttribute("username",session.getId());
            map.put("message", "success");
            map.put("sessionId",session.getId());
        }else {
            map.put("message", "fail");
        }
        return map;
    }
    @RequestMapping("/check")
    public Map<String,String> check(HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        return map;
    }
}
